import storage from '@/utils/storage'
const APP_NAME = process.env.VUE_APP_NAME
const KEY_USERINFO = APP_NAME ? `userInfo_eoc_${APP_NAME}` : 'userInfo'

function getUserInfoCache () {
  const storageCache = storage.get(KEY_USERINFO)
  if (!storageCache) {
    return {}
  }
  try {
    return JSON.parse(storageCache)
  } catch (e) {
    storage.set(KEY_USERINFO, {})
    return {}
  }
}

export let userInfoCache = getUserInfoCache()

export function setUserInfo (value) {
  userInfoCache = value
  storage.set(KEY_USERINFO, value)
}

export function removeUserInfo () {
  userInfoCache = {}
  storage.remove(KEY_USERINFO)
}

export function getUser () {
  return userInfoCache.user || {}
}

export function getRoles () {
  return userInfoCache.roles || {}
}

export function getMenu () {
  return userInfoCache.menu || []
}

export function getToken () {
  return userInfoCache.accessToken
}

export function getRefreshToken () {
  return userInfoCache.refreshToken
}
